﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using LBKDHelper.Models;
using System;
using System.ComponentModel;

namespace LM.YF.K3Cloud.Plugin.BillPlugins
{
    /// <summary>
    /// 鹿奔
    /// </summary>
    [HotUpdate, Description("产品原药价差表单插件")]
    public class RawMaterialPriceDifferentialBillPlugin : AbstractBillPlugIn
    {
        #region MyRegion

        /// <summary>
        /// 年份
        /// </summary>
        private readonly ElementKeySchema F_dev_year = new ElementKeySchema
        {
            DbKey = "F_DEV_YEAR",
            FormKey = "F_dev_year",
            OrmKey = "F_dev_year"
        };

        /// <summary>
        /// 月度
        /// </summary>
        private readonly ElementKeySchema F_dev_month = new ElementKeySchema
        {
            DbKey = "F_DEV_MONTH",
            FormKey = "F_dev_month",
            OrmKey = "F_dev_month"
        };

        /// <summary>
        /// 组织
        /// </summary>
        private readonly ElementKeySchema F_dev_org = new ElementKeySchema
        {
            DbKey = "F_DEV_ORG",
            FormKey = "F_dev_org",
            OrmKey = "F_dev_org"
        };

        /// <summary>
        /// 单据体
        /// </summary>
        private readonly ElementKeySchema FEntity = new ElementKeySchema
        {
            DbKey = "dev_t_MtrlPriceAdjustE",
            FormKey = "FEntity",
            OrmKey = "FEntity"
        };

        /// <summary>
        /// 单据体-物料编码
        /// </summary>
        private readonly ElementKeySchema F_dev_material = new ElementKeySchema
        {
            DbKey = "F_DEV_MATERIAL",
            FormKey = "F_dev_material",
            OrmKey = "F_dev_material"
        };

        /// <summary>
        /// 单据体-成本价
        /// </summary>
        private readonly ElementKeySchema F_dev_cost = new ElementKeySchema
        {
            DbKey = "F_DEV_COST",
            FormKey = "F_dev_cost",
            OrmKey = "F_dev_cost"
        };

        /// <summary>
        /// 单据体-市场价
        /// </summary>
        private readonly ElementKeySchema F_dev_base_price = new ElementKeySchema
        {
            DbKey = "F_dev_PriceNoTax",
            FormKey = "F_dev_PriceNoTax",
            OrmKey = "F_dev_PriceNoTax"
        };

        /// <summary>
        /// 单据体-价差
        /// </summary>
        private readonly ElementKeySchema F_dev_diff = new ElementKeySchema
        {
            DbKey = "F_DEV_DIFF",
            FormKey = "F_dev_diff",
            OrmKey = "F_dev_diff"
        };

        /// <summary>
        /// 单据体-价差（调整后）
        /// </summary>
        private readonly ElementKeySchema F_dev_diff2 = new ElementKeySchema
        {
            DbKey = "F_DEV_DIFF2",
            FormKey = "F_dev_diff2",
            OrmKey = "F_dev_diff2"
        };

        #endregion

        public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
        {
            switch (e.BarItemKey)
            {
                case "btnGetCostDiff":
                    LoadData();
                    break;
            }
        }

        private void LoadData()
        {
            var year = Convert.ToInt32(Model.GetValue(F_dev_year.OrmKey));
            var month = Model.GetValue(F_dev_month.OrmKey);
            var org = (Model.GetValue(F_dev_org.OrmKey) as DynamicObject)["Id"].ToString();
            //拆线呢最新市场价和成本价
            var dataList = DBUtils.ExecuteDynamicObject(Context, $@"select  
                b.F_QLTK_material, c.F_RDLV_QMJCDJ, b.F_QLTK_priceNoTax,b.F_QLTK_unit
                from QLTK_t_YYMarketPrice as a
                inner join QLTK_t_YYMarketPriceE as b on b.FID = a.FID
                left join (
                    select b2.F_RDLV_QMJCDJ, b2.F_RDLV_WLBM
                    from RDLV_t_GetSFCHZB as a2
                    inner join RDLV_t_GetSFCHZB_E as b2 on b2.FID = a2.FID
                    where a2.F_RDLV_YEAR = {year}
                    and a2.F_RDLV_MONTH = {month}
                    and a2.F_RDLV_ORGID = {org}
                ) as c on c.F_RDLV_WLBM = b.F_QLTK_material
                where a.F_QLTK_year = {year}
                and a.F_QLTK_Month = {month}
                and a.FUseOrgId = {org};");
            string ee = $@"select  
                b.F_QLTK_material, c.F_RDLV_QMJCDJ, b.F_QLTK_priceNoTax,b.F_QLTK_unit
                from QLTK_t_YYMarketPrice as a
                inner join QLTK_t_YYMarketPriceE as b on b.FID = a.FID
                left join (
                    select b2.F_RDLV_QMJCDJ, b2.F_RDLV_WLBM
                    from RDLV_t_GetSFCHZB as a2
                    inner join RDLV_t_GetSFCHZB_E as b2 on b2.FID = a2.FID
                    where a2.F_RDLV_YEAR = {year}
                    and a2.F_RDLV_MONTH = {month}
                    and a2.F_RDLV_ORGID = {org}
                ) as c on c.F_RDLV_WLBM = b.F_QLTK_material
                where a.F_QLTK_year = {year}
                and a.F_QLTK_Month = {month}
                and a.FUseOrgId = {org};";
            var dataListCount = dataList.Count;
            View.Model.DeleteEntryData("FEntity");
            for (var i = 0; i < dataListCount; i++)
            {
                var data = dataList[i];
                var materialId = data["F_QLTK_material"];
                var cost = Convert.ToDecimal(data["F_RDLV_QMJCDJ"]);//成本价
                var price = Convert.ToDecimal(data["F_QLTK_priceNoTax"]);//最新市场价
                var diff = price - cost;
                var unit = data["F_QLTK_unit"];//计量单位
                this.Model.CreateNewEntryRow("FEntity");
                View.Model.SetValue(F_dev_material.FormKey, materialId, i);
                View.Model.SetValue(F_dev_cost.FormKey, cost, i);
                View.Model.SetValue(F_dev_base_price.FormKey, price, i);
                View.Model.SetValue(F_dev_diff.FormKey, diff, i);
                View.Model.SetValue(F_dev_diff2.FormKey, diff, i);
                View.Model.SetValue("F_dev_unit", unit, i);
            }
            View.UpdateView(FEntity.FormKey);
        }
    }
}
